<template>
  <div class="saveUpdate">
    <el-dialog :title="DetailInfo.id?'编辑组员':'新增组员'" :visible.sync="showDialog" @close="$emit('update:showUpdate', false)"
               :showUpdate="showUpdate" width="600px">
      <el-form :model="DetailInfo" ref="saveChn" label-width="120px" size="mini">
        <dialog-wrapper>
          <el-form-item  label="组员名称:" prop="name" :rules="[{ required: true, message: '名称不能为空'}]">
            <el-input v-model="DetailInfo.name" :disabled="!!DetailInfo.id" ></el-input>
          </el-form-item>
          <el-form-item label="关联组长:" prop="superior" :rules="[{ required: true, message: '名称不能为空'}]">
            <el-select v-model="DetailInfo.superior"  placeholder="请选择">
              <el-option v-for="item in leaderlist" :key="item.id" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item v-if="DetailInfo.id" label="登录密码:" prop="password" :rules="[{ required: true, message: '密码不能为空'}]">
            <el-input v-model="DetailInfo.password"></el-input>
          </el-form-item>
          <el-form-item v-else label="登录密码:" >
            <el-input placeholder="系统自动生成" v-model="input" :disabled="true"></el-input>
          </el-form-item>
          <el-form-item label="备注:" prop="description">
            <el-input v-model="DetailInfo.description" type="textarea" :rows="2" ></el-input>
          </el-form-item>
        </dialog-wrapper>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="$emit('update:showUpdate', false)" size="small">取 消</el-button>
        <el-button v-if="DetailInfo.id" type="primary" :loading="loading" @click="updateDetail" size="small">确 定</el-button>
        <el-button v-else type="primary" :loading="loading" @click="saveDetail" size="small">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import DialogWrapper from '@/components/DialogWrapper'
  import { updateUserInfo, saveAddUserInfo } from '@/api'
  export default {
    components: { DialogWrapper },
    data() {
      return {
        showDialog: this.showUpdate,
        loading: false,
        DetailInfo: this.Detail,
        showParent: false,
        Parents: []
      }
    },
    props: {
      showUpdate: {
        type: Boolean,
        default: false
      },
      Detail: {
        type: Object
      },
      leaderlist: {
        type: Array
      },
      rolelist: {
        type: Object
      }
    },
    methods: {
      saveDetail(){
        this.$refs.saveChn.validate(valid => {
          if (valid) {
            saveAddUserInfo(this.DetailInfo).then(res => {
              if(res.status === 0){
                this.$message({ message: '新增成功。',type: 'success' })
                this.$emit('update:showUpdate', false)
              }
            })
          }
        })
      },
      updateDetail(){
        console.log(this.leaderlist)
        if(typeof this.DetailInfo.superior === "string") {
          for(let i=0;i<this.leaderlist.length;i++) {
            // console.log(this.DetailInfo.superior)
            if (this.leaderlist[i]['name'] === this.DetailInfo.superior) {
              this.DetailInfo.superior = this.leaderlist[i]['id']
            }
            // console.log(this.DetailInfo.superior)
          }
        }
        this.$refs.saveChn.validate(valid => {
          if (valid) {
            let {password, superior, description} = this.DetailInfo
            updateUserInfo({password, superior, description, "role_type":16}, this.Detail.id).then(res => {
              if(res.status === 0){
                this.$message({ message: '编辑成功。',type: 'success' })
                this.$emit('update:showUpdate', false)
              }
            })
          }
        })
      },
    },
    watch: {
      showUpdate() {
        this.showDialog = this.showUpdate
      }
    }
  }
</script>
